import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class, _class2;

/**
 * Template - 通用模板
 * @since 2018-08-24
 * @version 0.0.1

 */
import React, { Component } from 'react';
import notification from 'utils/notification';
import { Button, Form } from 'antd';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import { getCurrentOrganizationId, filterNullValueObject } from 'utils/utils';
import FilterForm from "./FilterForm";
import ListTable from "./ListTable";
/**
 * 通用模板头页面
 * @extends {Component} - React.Component
 * @reactProps {Object} form - 表单对象
 * @reactProps {Function} [dispatch=function(e) {return e;}] - redux dispatch方法
 * @return React.element
 */

var List = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = connect(function (_ref) {
  var template = _ref.template,
      loading = _ref.loading;
  return {
    template: template,
    loading: {
      query: loading.effects['template/query']
    },
    organizationId: getCurrentOrganizationId()
  };
}), _dec3 = formatterCollections({
  code: ['cimp.template']
}), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(List, _Component);

  var _super = _createSuper(List);

  function List() {
    _classCallCheck(this, List);

    return _super.apply(this, arguments);
  }

  _createClass(List, [{
    key: "componentDidMount",
    value:
    /**
     * componentDidMount 生命周期函数
     * render()执行后获取页面数据
     */
    function componentDidMount() {
      var pagination = this.props.template.pagination;
      this.fetchList(pagination);
    }
    /**
     * fetchList - 查询列表数据
     * @param {object} [params = {}] - 查询参数
     */

  }, {
    key: "fetchList",
    value: function fetchList() {
      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          organizationId = _this$props.organizationId,
          form = _this$props.form;
      var fieldValues = filterNullValueObject(form.getFieldsValue());
      dispatch({
        type: 'template/query',
        payload: _objectSpread({
          page: _isEmpty(fields) ? {} : fields,
          organizationId: organizationId
        }, fieldValues)
      });
    }
    /**
     * 新增模板，跳转到明细页面
     */

  }, {
    key: "handleAddTemplate",
    value: function handleAddTemplate() {
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cimp/template/detail/create"
      }));
    }
    /**
     * 修改通用模板信息，跳转到明细页面
     * @param {object} record - 消息模板对象
     */

  }, {
    key: "handleEditContent",
    value: function handleEditContent(record) {
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cimp/template/detail/".concat(record.id)
      }));
    }
    /**
     * 删除通用模板信息后刷新表格数据列表
     * @param {object} record - 消息模板对象
     */

  }, {
    key: "handleDelContent",
    value: function handleDelContent(record) {
      var _this = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          organizationId = _this$props2.organizationId,
          _this$props2$template = _this$props2.template.pagination,
          pagination = _this$props2$template === void 0 ? {} : _this$props2$template;
      dispatch({
        type: 'template/removeHeader',
        payload: _objectSpread(_objectSpread({}, record), {}, {
          organizationId: organizationId
        })
      }).then(function (res) {
        if (res) {
          notification.success({
            message: intl.get("cpaas.common.notification.success.delete").d('删除成功')
          });

          _this.fetchList(pagination);
        }
      });
    }
    /**
     * 设置Form
     * @param {object} ref - FilterForm组件引用
     */

  }, {
    key: "handleBindRef",
    value: function handleBindRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.form = (ref.props || {}).form;
    }
    /**
     * render
     * @returns React.element
     */

  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props3 = this.props,
          form = _this$props3.form,
          loading = _this$props3.loading,
          organizationId = _this$props3.organizationId,
          _this$props3$template = _this$props3.template,
          headerList = _this$props3$template.headerList,
          pagination = _this$props3$template.pagination;
      var filterProps = {
        form: form,
        search: this.fetchList
      };
      var listProps = {
        pagination: pagination,
        organizationId: organizationId,
        dataSource: headerList,
        loading: loading.query,
        onChange: function onChange(page) {
          return _this2.fetchList(page);
        },
        editContent: this.handleEditContent,
        delContent: this.handleDelContent
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get("cimp.template.view.message.title").d('导入模板管理')
      }, /*#__PURE__*/React.createElement(Button, {
        icon: "plus",
        type: "primary",
        onClick: this.handleAddTemplate
      }, intl.get("cpaas.common.button.create").d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(FilterForm, filterProps)), /*#__PURE__*/React.createElement(ListTable, listProps)));
    }
  }]);

  return List;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "fetchList", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddTemplate", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddTemplate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEditContent", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEditContent"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelContent", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelContent"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleBindRef", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleBindRef"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { List as default };